<template>
  <div class="translateContain">
    <div class="translateLeft">
      <el-button @click="gotoEnglish"><i class="el-icon-back" ></i></el-button><br/>
      <iframe v-if="translateUrl != null" :src="translateUrl" width="100%" height="800px" ref="iframe"></iframe>
    </div>
    <div class="translateRight">
      单词本：<el-select v-model="value" placeholder="请选择">
        <el-option
          v-for="item in books"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <span style="float: left">{{ item.label }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
        </el-option>
      </el-select>&nbsp;
      <el-button type="primary" @click="reset">新建</el-button><br/><br/>
      <el-input
        placeholder="请输入需要翻译的单词"
        suffix-icon="el-icon-edit"
        v-model="word"
        style="width: 350px">
      </el-input>&nbsp;
      <el-button type="danger">翻译</el-button>
    </div>
    <!-- 添加或修改单词本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="单词本名称" prop="bookName">
          <el-input v-model="form.bookName" type="input" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="单词本简介" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addBook, updateBook} from "@/api/english/book";

export default {
  name: "translate",
  data() {
    return {
      translateUrl: {
        type: String,
        default() {
          return null;
        }
      },
      books: [{
        value: 'Beijing',
        label: '北京'
      }, {
        value: 'Shanghai',
        label: '上海'
      }, {
        value: 'Nanjing',
        label: '南京'
      }, {
        value: 'Chengdu',
        label: '成都'
      }, {
        value: 'Shenzhen',
        label: '深圳'
      }, {
        value: 'Guangzhou',
        label: '广州'
      }],
      value: "",
      word: "",
      iframeWin: null,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
    }
  },
  created() {
    if(this.$route.query.translateUrl !== undefined){
      this.translateUrl = this.$route.query.translateUrl;
    }else{

    }
    this.enterKeyup();
  },
  mounted() {
    this.iframeWin = this.$refs.iframe.contentWindow;
  },
  destroyed() {
    // 销毁enter事件
    this.enterKeyupDestroyed();
  },
  methods: {
    gotoEnglish(){
      this.$router.push({ path: "/english"});
    },
    //键盘按键
    enterKey(event) {
      const componentName = this.$options.name;
      if (componentName == "translate") {
        const code = event.keyCode
          ? event.keyCode
          : event.which
            ? event.which
            : event.charCode;
        if (code == 38) {
          this.translateWord();
        }
      }
    },
    enterKeyupDestroyed() {
      document.removeEventListener("keyup", this.enterKey);
    },
    enterKeyup() {
      document.addEventListener("keyup", this.enterKey);
    },
    translateWord(){
      let text = this.iframeWin.document.getElementById('1');
      console.log(text);
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: null,
        userId: null,
        info: null,
        delFlag: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookId != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
}
</script>

<style scoped>
.translateContain{
  display: flex;
}
.translateLeft{
  width: 70%;
  margin-right: 10px;
}
.translateRight{
  width: 30%;
  margin-left: 10px;
}
</style>
